package com.zrwl.poscloud.xo.service.impl;
import com.zrwl.poscloud.base.entity.HomeworkSubmit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import com.zrwl.poscloud.xo.mapper.HomeworkSubmitMapper;
import com.zrwl.poscloud.xo.service.HomeworkSubmitService;
import com.zrwl.poscloud.xo.vo.HomeworkSubmitVO;

import javax.annotation.Resource;

/**
 * 作业提交记录 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-17
 */
@Service
public class HomeworkSubmitServiceImpl extends SuperServiceImpl<HomeworkSubmitMapper,HomeworkSubmit> implements HomeworkSubmitService {

    @Autowired
    private HomeworkSubmitService homeworkSubmitService;
    @Resource
    private HomeworkSubmitMapper homeworkSubmitMapper;
    @Override
    public IPage<HomeworkSubmitVO> getPageList(HomeworkSubmitVO homeworkSubmitVO){
       Page<HomeworkSubmit> page = new Page<>();
       page.setCurrent(homeworkSubmitVO.getCurrentPage());
       page.setSize(homeworkSubmitVO.getPageSize());
       IPage<HomeworkSubmitVO> pageList = homeworkSubmitMapper.getPageList(page, homeworkSubmitVO);
       List<HomeworkSubmitVO> list = pageList.getRecords();
       pageList.setRecords(list);
       return pageList;
}
    @Override
    public List<HomeworkSubmit> getAllList(HomeworkSubmitVO homeworkSubmitVO){
        QueryWrapper<HomeworkSubmit> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<HomeworkSubmit> list = homeworkSubmitService.list(queryWrapper);
        return list;
}
    @Override
    public String add(HomeworkSubmitVO homeworkSubmitVO){
        HomeworkSubmit entity = new HomeworkSubmit();
        BeanUtils.copyProperties(homeworkSubmitVO, entity, SysConf.STATUS);
        entity.insert();
        homeworkSubmitVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);}
    @Override
    public String edit(HomeworkSubmitVO homeworkSubmitVO){
        HomeworkSubmit entity = homeworkSubmitService.getById(homeworkSubmitVO.getUid());
        BeanUtils.copyProperties(homeworkSubmitVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}
    @Override
    public String delete(HomeworkSubmitVO homeworkSubmitVO){
        HomeworkSubmit entity = homeworkSubmitService.getById(homeworkSubmitVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
}

    @Override
    public HomeworkSubmitVO getHomeworkSubmitByBatchAndDetail(String batchUid, String detailUid) {
        QueryWrapper<HomeworkSubmit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_uid",batchUid);
        queryWrapper.eq("detail_uid",detailUid);
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        queryWrapper.last("limit 1");
        HomeworkSubmit entity = homeworkSubmitService.getOne(queryWrapper);
        HomeworkSubmitVO homeworkSubmitVO = new HomeworkSubmitVO();
        if(entity != null){
            BeanUtils.copyProperties(entity,homeworkSubmitVO);
        }
        return homeworkSubmitVO;
    }
}
